package com.example.administrator.hqq.activity.register;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.administrator.hqq.MainActivity;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.activity.module.AddModuleActivity;
import com.example.administrator.hqq.adapter.SetOnClickItem;
import com.example.administrator.hqq.adapter.register.HobbiesAdapter;
import com.example.administrator.hqq.adapter.register.MessageHeadAdater;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.base.MyApplication;
import com.example.administrator.hqq.bean.MessageBean;
import com.example.administrator.hqq.bean.RegisterBeen;
import com.example.administrator.hqq.bean.event.RegisterEvent;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.example.administrator.hqq.utils.HintWordsUtils;
import com.example.administrator.hqq.utils.RongIMUtils;
import com.example.administrator.hqq.utils.SharedPrefsStrListUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * @author 康海涛
 * @describe:模块采集
 */
public class HobbiesActivity extends MyActivity {

    @BindView(R.id.app_return)
    ImageView appReturn;
    @BindView(R.id.hobbies_rec)
    GridView hobbiesRec;
    @BindView(R.id.start_activity)
    Button startActivity;
    private MessageHeadAdater messageHeadAdater;
    private SetOnClickItem setOnClickItem;
    private List<MessageBean.DataBean> data;
    private String TAG = this.getClass().getSimpleName();
    private MessageBean messageBean;
    private List<String> hobbies;
    private String topic = "";
    private HobbiesAdapter hobbiesAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hobbies);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initview();
        initdata();
    }

    private void initview() {
        data = new ArrayList<>();
        hobbies = new ArrayList();
        hobbiesAdapter = new HobbiesAdapter(this, data);
        hobbiesRec.setSelector(new ColorDrawable(Color.TRANSPARENT));
        hobbiesRec.setAdapter(hobbiesAdapter);
        hobbiesRec.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == data.size()&&data.size()<12) {
                    Intent intent = new Intent(HobbiesActivity.this, AddModuleActivity.class);
                    intent.putExtra(LoongsConfig.phone,getIntent().getStringExtra(LoongsConfig.phone));
                    intent.putExtra(LoongsConfig.user_name,getIntent().getStringExtra(LoongsConfig.user_name));
                    intent.putExtra(LoongsConfig.birth,getIntent().getStringExtra(LoongsConfig.birth));
                    intent.putExtra(LoongsConfig.sex,getIntent().getStringExtra(LoongsConfig.sex));
                    intent.putExtra(LoongsConfig.url,getIntent().getStringExtra(LoongsConfig.url));
                    intent.putExtra(LoongsConfig.type,AddModuleActivity.Type.REGISTER);
                    intent.putExtra(LoongsConfig.hobbies, (Serializable) hobbies);
                    startActivity(intent);
                    finish();
                } else {
                    data.get(i).setSelect(!data.get(i).isSelect());
                    if (data.get(i).isSelect())
                    {
                        hobbies.add(messageBean.getData().get(i).getId());
                    }else
                    {
                        hobbies.remove(messageBean.getData().get(i).getId());
                    }
                    if (hobbies.size()>0)
                    {
                        startActivity.setSelected(true);
                        startActivity.setText("进入话圈圈");
                        startActivity.setTextColor(Color.parseColor("#000000"));
                    }
                    else{
                        startActivity.setSelected(false);
                        startActivity.setText("至少选择一个模块");
                        startActivity.setTextColor(Color.parseColor("#ffffff"));
                    }
                    hobbiesAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void initdata() {
        OkHttpUtils.get().url(UrlFactory.getHobbies())
                .addParams(LoongsConfig.gender, getIntent().getStringExtra(LoongsConfig.sex))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "话题onError: " + e.getMessage());
            }

            @Override
            public void onResponse(String s, int id) {
                Log.e(TAG, "话题onResponse: " + s);
                Gson gson = new Gson();
                messageBean = gson.fromJson(s, MessageBean.class);
                if (messageBean.getCode() == 200) {
                    data.clear();
                    data.addAll(messageBean.getData());
                    hobbiesAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @OnClick({R.id.app_return, R.id.start_activity})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.app_return:
                finish();
                break;
            case R.id.start_activity:
                if (hobbies.size()<=0)
                {
                    return;
                }
                for (int i = 0; i < hobbies.size(); i++) {
                    if (i == 0) {
                        topic = hobbies.get(i);
                    } else {
                        topic = topic + "," + hobbies.get(i);
                    }
                }
                Log.e(TAG, "topic: "+topic );
                Log.e(TAG, "onViewClicked: "+ getIntent().getStringExtra(LoongsConfig.birth));
                GetBuilder tag = OkHttpUtils.get().url(UrlFactory.register()).tag(this);
                Log.e(TAG, "onViewClicked: " + getIntent().getStringExtra(LoongsConfig.phone));
                tag.addParams(LoongsConfig.phone, getIntent().getStringExtra(LoongsConfig.phone))
                        .addParams(LoongsConfig.user_id,SPUtils.getInstance().getString(LoongsConfig.user_id))
                        .addParams(LoongsConfig.nickname, getIntent().getStringExtra(LoongsConfig.user_name))
                        .addParams(LoongsConfig.birth, getIntent().getStringExtra(LoongsConfig.birth))
                        .addParams(LoongsConfig.gender, getIntent().getStringExtra(LoongsConfig.sex))
                        .addParams(LoongsConfig.portrait_id, getIntent().getStringExtra(LoongsConfig.url))
                        .addParams(LoongsConfig.topic, topic)
                        .build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "注册onError: " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String s, int id) {
                        Log.e(TAG, "注册onResponse: " + s);
                        Gson gson = new Gson();
                        RegisterBeen registerBeen = gson.fromJson(s, RegisterBeen.class);
                        HintWordsUtils.showMarkeWords(registerBeen.getCode(),registerBeen.getDescription());
                        if (registerBeen.getCode()==200)
                        {
                            String login_token = registerBeen.getLogin_token();
                            String rongyun_token = registerBeen.getRongyun_token();
                            String gender = registerBeen.getGender();
                            SPUtils.getInstance().put(LoongsConfig.gender,gender);
                            SPUtils.getInstance().put( LoongsConfig.loginToken, login_token);
                            SPUtils.getInstance().put( LoongsConfig.rongToken, rongyun_token);
                            Intent intent = new Intent(HobbiesActivity.this, MainActivity.class);
                            startActivity(intent);
                        }
                    }
                });
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(RegisterEvent registerEvent){
        switch (registerEvent.getType()) {
            case BACK:
                finish();
                break;
        }
    }
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }
}
